#include "Serializable.h"

#include <QObject>
#include <QMessageBox>
#include <QDir>
#include <QTextStream>



namespace
{
QString fs_BASE_NAME = "New Campaign";
}



Serializable::Serializable()
{
    data = new Data;
}



Serializable::Serializable(const Serializable& other)
{
    setFilePath(other.filePath());
}



void Serializable::setFilePath(const QString& file_path)
{
    data->file_info.setFile(file_path);
}



QString Serializable::filePath() const
{
    return data->file_info.absoluteFilePath();
}



QFileInfo Serializable::fileInfo() const
{
    return data->file_info;
}


void Serializable::setLastError(const QString &error)
{
    data->last_error = error;
}


QString Serializable::error() const
{
    return data->last_error;
}


void Serializable::setDirty(const bool& dirty)
{
    if (dirty)
    {
        dirty;
    }
    data->dirty = dirty;
}


bool Serializable::isDirty() const
{
    return data->dirty;
}


bool Serializable::save(Serializable* serialized)
{
    if (serialized->serializedData().isEmpty())
    {
        QString error = QT_TR_NOOP("Save Error: Written data is empty!");
        serialized->setLastError(error);
        return false;
    }


    if (serialized->filePath().isEmpty())
    {
        QString error = QT_TR_NOOP("Save Error: Empty file name!");
        serialized->setLastError(error);
        return false;
    }


    QFile file(serialized->filePath());
    if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
    {
        QString error = QString("%1:\n'%2'?")
                        .arg(QT_TR_NOOP("Failed to open file"))
                        .arg(serialized->filePath());
        serialized->setLastError(error);
        return false;
    }

    QString error = QString("%1:\n'%2'?")
                    .arg(QT_TR_NOOP("File opened successfully"))
                    .arg(serialized->filePath());
    serialized->setLastError(error);

    file.write(serialized->serializedData().toUtf8());
    serialized->setDirty(false);

    QDir::setCurrent(serialized->fileInfo().absolutePath());
    return true;
}



bool Serializable::open(Serializable* deserialized, const QString& file_path)
{
    if (!deserialized) return false;


    if (file_path.isEmpty())
    {
        QString error = QT_TR_NOOP("Open Error: Empty file name!");
        deserialized->setLastError(error);
        return false;
    }

    QFile file(file_path);
    if (!file.exists())
    {
        QString error = QString("%1 '%2'")
                        .arg(QT_TR_NOOP("Open Error: File does not exist!"))
                        .arg(file_path);
        deserialized->setLastError(error);
        return false;
    }


    if (!file.open(QIODevice::ReadOnly))
    {
        QString error = QString("%1 '%2'")
                        .arg(QT_TR_NOOP("Open Error: Failed to open file!"))
                        .arg(file_path);
        deserialized->setLastError(error);
        return false;
    }

    QString error = QString("%1 '%2'")
                    .arg(QT_TR_NOOP("Open Success: File open succeeded!"))
                    .arg(file_path);
    deserialized->setLastError(error);
    deserialized->setSerializedData(QString::fromUtf8(file.readAll()));
    deserialized->setFilePath(file_path);



    deserialized->setDirty(false);
    QDir::setCurrent(deserialized->fileInfo().absolutePath());
    return true;
}



